<template>
  <div class="artdetail">
    <!-- {{ articleData.title }} -->
    <!-- 普通文章： -->
    <div @click="$router.push('/mycomment')">去我的跟帖</div>
    <!-- <rouser-link to="/mycomment">去我的帖子</rouser-link>  为什么不能用这个？？ -->
    <div class="normalArticle" v-if="articleData.type == 1">
      <div class="header">
        <span class="iconfont iconjiantou2"></span>
        <span class="iconfont iconnew"></span>
        <div
          class="btnFollow"
          @click="handleFollow"
          :class="{
            unFollow: !articleData.has_follow,
          }"
        >
          {{ articleData.has_follow ? "已关注" : "关注" }}
        </div>
      </div>
      <div class="title">
        {{ articleData.title }}
      </div>
      <div class="info">
        <div class="author">{{ articleData.user.nickname }}</div>
        <div class="date">2020-12-12</div>
      </div>
      <div class="maincontent" v-html="articleData.content"></div>
    </div>

    <!-- 有视频的文章： -->
    <div class="videoArticle" v-if="articleData.type == 2">
      显示的是有视频的文章呀~~~
      <video
        poster="http://157.122.54.189:9083/uploads/image/IMG1606459976647.jpeg"
        controls
        src="https://video.pearvideo.com/mp4/adshort/20200421/cont-1670293-15098199_adpkg-ad_hd.mp4"
      ></video>
      <div class="info">
        <img
          :src="$axios.defaults.baseURL + articleData.user.head_img"
          alt=""
          class="userpic"
        />
        <div class="author">
          {{ articleData.user.nickname }}
        </div>
        <div
          class="btnFollow"
          @click="handleFollow"
          :class="{
            unFollow: !articleData.has_follow,
          }"
        >
          {{ articleData.has_follow ? "已关注" : "关注" }}
        </div>
      </div>
      <div class="title">
        {{ articleData.title }}
      </div>
    </div>
    <!-- 点赞与分享呀： -->
    <div class="touch">
      <div
        class="dianzan"
        @click="handleLike"
        :class="{
          hasLike: this.articleData.has_like,
        }"
      >
        <span class="iconfont icondianzan"> </span>
        {{ articleData.like_length }}
      </div>
      <div class="share">
        <span class="iconfont iconweixin"></span>
        微信
      </div>
    </div>
    <!-- 在这里插入评论列表 -->
    <h1 class="commentTitle">精彩评论</h1>
    <commentMain
      :commentDate="comment"
      v-for="comment in commentList"
      :key="comment.id"
    />
    <!-- 将超过3条评论的内容隐藏到“更多更贴里面” -->
    <div
      class="btnMoreComment"
      @click="$router.push('/morecomment/' + $route.params.id)"
    >
      更多跟帖
    </div>
    <CommentIput @reloadComment="reloadComment" />
  </div>
</template>

<script>
import CommentIput from "../components/Comment/Input";
import commentMain from "../components/Comment/Main";
export default {
  data() {
    return {
      articleData: {},
      commentList: [],
    };
  },
  components: {
    commentMain,
    CommentIput,
  },
  created() {
    this.$axios({
      url: "/post/" + this.$route.params.id,
    }).then((res) => {
      console.log(res.data);
      this.articleData = res.data.data;
      console.log(res.data.data, "能获取@作者么？？");
    });
    // //初始化页面时就加载评论列表：
    // this.$axios({
    //   url: "/post_comment/" + this.$route.params.id,
    // }).then((res) => {
    //   console.log(res.data);
    //   if (res.data.data.length > 3) {
    //     res.data.data.length = 3;
    //   }
    //   this.commentList = res.data.data;
    // });   把“加载评论” 封装成为一个函数，两处调用
    this.reloadComment();
  },
  methods: {
    reloadComment() {
      //初始化页面时就加载评论列表：
      this.$axios({
        url: "/post_comment/" + this.$route.params.id,
      }).then((res) => {
        console.log(res.data);
        if (res.data.data.length > 3) {
          res.data.data.length = 3;
        }
        this.commentList = res.data.data;
      });
    },
    handleFollow() {
      if (this.articleData.has_follow) {
        // 如果已经关注：点击按钮，就会是取消关注：
        this.$axios({
          url: "/user_unfollow/" + this.articleData.user.id,
        }).then((res) => {
          console.log(res.data);
          this.articleData.has_follow = false;
        });
      } else {
        // 如果没有关注：就会时关注
        this.$axios({
          url: "/user_follows/" + this.articleData.user.id,
        }).then((res) => {
          console.log(res.data);
          this.articleData.has_follow = true;
        });
      }
    },
    // 处理点赞事件：
    handleLike() {
      this.$axios({
        url: "/post_like/" + this.$route.params.id,
      }).then((res) => {
        console.log(res.data);
        const { message } = res.data;
        if (message == "点赞成功") {
          this.articleData.has_like = true;
          this.articleData.like_length++;
          this.$toast.success("作者已收到你的点赞");
        }
        if (message == "取消成功") {
          this.articleData.has_like = false;
          this.articleData.like_length--;
          this.$toast("已取消点赞");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.artdetail {
  padding-bottom: 80 / 360 * 100vw;
}
.commentTitle {
  text-align: center;
  // font-weight: 700;  h1本来就是加粗的，所以不需要加粗
  padding: 20 / 360 * 100vw;
  font-size: 18 / 360 * 100vw;
}
.btnMoreComment {
  text-align: center;
  font-size: 18 / 360 * 100vw;
  width: 121 / 360 * 100vw;
  height: 30 / 360 * 100vw;
  line-height: 30 / 360 * 100vw;
  border: 1px solid #888;
  margin: 30 / 360 * 100vw auto;
  // margin-bottom: 80 / 360 * 100vw;
  border-radius: 15 / 360 * 100vw;
  color: #666;
}
.normalArticle {
  // 内边距：上右下（左可以省略~~）
  padding: 20 / 360 * 100vw 20 / 360 * 100vw 30 / 360 * 100vw;

  border-bottom: 5px solid #cccc;
  .header {
    display: flex;
    align-items: center;
    .iconjiantou2 {
      font-weight: 700;
      color: #4d4d4d;
      font-size: 15 / 360 * 100vw;
    }
    .iconnew {
      flex-grow: 1;
      padding-left: 10 / 360 * 100vw;
      font-size: 54 / 360 * 100vw;
    }
    .btnFollow {
      padding: 0 13 / 360 * 100vw;
      height: 26 / 360 * 100vw;
      line-height: 26 / 360 * 100vw;
      border: 1px solid #8888;
      border-radius: 13 / 360 * 100vw;
      font-size: 12 / 360 * 100vw;
      &.unFollow {
        background-color: red;
        border: red;
        color: #ffff;
      }
    }
  }
  .title {
    font-size: 20 / 360 * 100vw;
    font-weight: 700;
  }
  .info {
    display: flex;
    margin-bottom: 30 / 360 * 100vw;
    line-height: 26 / 360 * 100vw;
    font-size: 13 / 360 * 100vw;
    color: #868686;
    .date {
      padding-left: 20 / 360 * 100vw;
    }
  }
  .maincontent {
    color: #4d4d4d;
    line-height: 26 / 360 * 100vw;
    font-size: 13 / 360 * 100vw;
    /deep/ img {
      max-width: 100%;
    }
  }
}
.videoArticle {
  padding: 20 / 360 * 100vw 20 / 360 * 100vw 30 / 360 * 100vw;
  // padding-bottom: 30 / 360 * 100vw;
  border-bottom: 5px solid #cccc;
  video {
    // width: 100 / 360 * 100vw;
    width: 100%;
  }
  .info {
    display: flex;
    align-items: center;
    margin-bottom: 30 / 360 * 100vw;
    font-size: 14 / 360 * 100vw;
    .userpic {
      width: 40 / 360 * 100vw;
      height: 40 / 360 * 100vw;
      border-radius: 50%;
      object-fit: cover;
    }
    .author {
      flex-grow: 1;
      padding-left: 10 / 360 * 100vw;
      color: #868686;
    }
    .btnFollow {
      padding: 0 13 / 360 * 100vw;
      height: 26 / 360 * 100vw;
      line-height: 26 / 360 * 100vw;
      border: 1px solid #8888;
      color: #4d4d4d;
      border-radius: 13 / 360 * 100vw;
      // font-size: 10 / 360 * 100vw;
      &.unFollow {
        background-color: red;
        border: red;
        color: #ffff;
      }
    }
  }
  .title {
    font-size: 16 / 360 * 100vw;
    padding: 10 / 360 * 100vw;
    color: #333;
  }
}
.touch {
  display: flex;
  justify-content: space-evenly;
  padding: 20 / 360 * 100vw;
  border-bottom: 4 / 360 * 100vw solid #e4e4e4;
  .dianzan,
  .share {
    // margin-top: 40 / 360 * 100vw;
    height: 30 / 360 * 100vw;
    line-height: 30 / 360 * 100vw;
    // width: 75 / 360 * 100vw;
    font-size: 13 / 360 * 100vw;
    padding: 0 15 / 360 * 100vw;
    border: 1px solid #8888;
    border-radius: 15 / 360 * 100vw;
    .iconfont {
      padding-right: 2px;
    }
    .icondianzan {
      font-size: 16 / 360 * 100vw;
    }
    .iconweixin {
      font-size: 16 / 360 * 100vw;
      color: #00c800;
    }
    &.hasLike {
      color: orange;
    }
  }
}
</style>